package com.example.maruko_course_backend.controller;

import com.example.maruko_course_backend.criterias.PageConditionCriteria;
import com.example.maruko_course_backend.criterias.PageCriteria;
import com.example.maruko_course_backend.criterias.SentenceCriteria;
import com.example.maruko_course_backend.criterias.SentenceKnowledgeCriteria;
import com.example.maruko_course_backend.dto.*;
import com.example.maruko_course_backend.entity.Sentence;
import com.example.maruko_course_backend.services.EditSentenceService;
import com.example.maruko_course_backend.services.SentenceService;
import com.example.maruko_course_backend.vo.SentenceKnowledgeVo;
import com.example.maruko_course_backend.vo.SentenceModifiedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import static com.example.maruko_course_backend.constants.CommonConstants.FAIL;
import static com.example.maruko_course_backend.constants.CommonConstants.SUCCESS;

@RestController
@RequestMapping("/sentences")
public class SentenceController {

  @Autowired
  private SentenceService sentenceService;
  
  @Autowired
  private EditSentenceService editSentenceService;

  @PostMapping("/knowledgeVo")
  public ResponseEntity<SentenceKnowledgeVo> getOneSentenceRelatedKnowledgeItems(@RequestBody SentenceKnowledgeCriteria criteria) {
    return ResponseEntity.ok().body(sentenceService.findVoBySentenceKnowledgeCriteria(criteria));
  }
  
  @GetMapping("/{videoNum}/{seq}")
  public ResponseEntity<SentenceModifiedVo> findSentenceModifiedVo(@PathVariable String videoNum, @PathVariable String seq) {
    return ResponseEntity.ok().body(sentenceService.findSentenceModifiedVo(videoNum, seq));
  }
  
  @PostMapping("/{videoNum}/{seq}")
  public ResponseEntity<String> saveOrModifyEditedWord(@PathVariable String videoNum, @PathVariable String seq, @RequestBody EditedWord editedWord) {
    Sentence sentence = sentenceService.saveOrModifyWord(videoNum, seq, editedWord);
    if (sentence == null) {
      return ResponseEntity.ok().body(FAIL);
    }
    return ResponseEntity.ok().body(SUCCESS);
  }
  
  @PostMapping("/{videoNum}/{seq}/grammar")
  public ResponseEntity<String> saveOrModifyEditedGrammar(@PathVariable String videoNum, @PathVariable String seq, @RequestBody EditedGrammar editedGrammar) {
    Sentence sentence = editSentenceService.saveOrModifyGrammar(videoNum, seq, editedGrammar);
    if (sentence == null) {
      return ResponseEntity.ok().body(FAIL);
    }
    return ResponseEntity.ok().body(SUCCESS);
  }
  
  @DeleteMapping("/{videoNum}/{seq}/{wordContent}")
  public ResponseEntity<String> deleteEditedWord(@PathVariable String videoNum, @PathVariable String seq, @PathVariable String wordContent) {
    return ResponseEntity.ok(sentenceService.deleteWord(videoNum, seq, wordContent));
  }

  @PostMapping("/page")
  public ResponseEntity<PageDto<SentenceDto>> findAllSentenceDtosByPage(@RequestBody PageCriteria pageCriteria) {
    return ResponseEntity.ok().body(sentenceService.findAllSentenceDtosByPage(pageCriteria));
  }

  @PostMapping("/page/condition")
  public ResponseEntity<PageDto<SentenceDto>> findSentencesByPageCondition(@RequestBody PageConditionCriteria<SentenceCriteria> criteria) {
    return ResponseEntity.ok().body(sentenceService.findAllSentenceDtosByPageCondition(criteria));
  }

  @PostMapping("/uploadExcel")
  public UploadResultDto uploadExcel(@RequestParam("file") MultipartFile file) throws IOException {
    String fileName = file.getOriginalFilename(); // 获取文件名
    InputStream is = null;
    try {
      is = file.getInputStream();
      List<Map> uploadSentenceDtoMap = sentenceService.getListByExcel(is, fileName);// 获取解析后的List集合
      Boolean result = sentenceService.batchImportSentences(uploadSentenceDtoMap); // 把数据插入数据库
      if (result) {
        return new UploadResultDto("200", "文件上传成功！");
      } else {
        return new UploadResultDto("500", "文件上传失败！");
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      is.close();
    }
    return new UploadResultDto("400", "文件错误！");
  }
}
